/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-11-20 13:44:54
 * @LastEditors: GGB
 * @LastEditTime: 2021-11-20 14:05:53
 */
import { Injectable } from '@nestjs/common';
import { v4 as uuid } from "uuid";
import { students } from '../db';
import { CreateStudentDto, FindStudentsResponseDto, StudentResponseDto, UpdateStudentDto } from './dto/student.dto';


@Injectable()
export class StudentService {
    private students = students;
    /**
     * 获取所有学生列表
     * @returns FindStudentsResponseDto
     */
    getStudents(): FindStudentsResponseDto[] {
        return this.students;
    }
    /**
     * 根据学生ID查询学生信息
     * @param id 学生id
     * @returns 
     */
    getStudentById(id: string): FindStudentsResponseDto {
        return this.students.find(student => student.id === id)
    }
    /**
     * 创建学生
     * @param payload 新增的学生id
     * @returns 
     */
    createStudent(payload: CreateStudentDto): StudentResponseDto {
        let newStudent = {
            id: uuid(),
            ...payload
        }
        this.students.push(newStudent)
        return newStudent;
    }
    /**
     * 根据用户id更新学生信息
     * @param payload 更新信息
     * @param id 用户id
     * @returns 
     */
    updateStudent(payload: UpdateStudentDto, id: string): StudentResponseDto {
        let updatedStudent: StudentResponseDto
        let updatedStudentList = this.students.map(student => {
            if (student.id === id) {
                updatedStudent = {
                    id,
                    ...payload
                }
                return updatedStudent
            } else return student
        })
        this.students = updatedStudentList;
        return updatedStudent;
    }
    /**
     * 根据教师id查询学生列表
     * @param teacherId 教师id
     * @returns 
     */
    getStudentByTeacherId(teacherId: string): FindStudentsResponseDto[] {
        return this.students.filter(student => student.teacher === teacherId)
    }
    /**
     * 更新学生的教师信息
     * @param teacherId 教师id
     * @param studentId 学生id
     * @returns 
     */
    updateStudentTeacher(teacherId: string, studentId: string): StudentResponseDto {
        let updatedStudent: StudentResponseDto

        let updatedStudentList = this.students.map(student => {
            if (student.id === studentId) {
                updatedStudent = {
                    ...student,
                    teacher: teacherId
                };
                return updatedStudent
            } else return student
        });

        this.students = updatedStudentList

        return updatedStudent
    }
}
